package com.hdu.seckilldemo.rabbitmq;

import com.hdu.seckilldemo.pojo.SeckillMessage;
import com.hdu.seckilldemo.pojo.User;
import com.hdu.seckilldemo.service.IGoodsService;
import com.hdu.seckilldemo.service.IOrderService;
import com.hdu.seckilldemo.utils.JsonUtil;
import com.hdu.seckilldemo.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * MQ消费者
 */
@Slf4j
@Service
public class MQReceiver {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;
    
    @RabbitListener(queues = "seckillQueue")
    public void seckilll_handler(String msg) throws InterruptedException {
        SeckillMessage message = (SeckillMessage) JsonUtil.jsonStr2Object(msg,SeckillMessage.class);
        log.info("接收到的消息"+msg);
        Long goodsId = message.getGoodsId();
        User user = message.getUser();
        //根据goodsId查询商品详情
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);

        //下单操作
        orderService.seckill(user,goodsVo);
    }

/*    @RabbitListener(queues = "queue")
    public void recive(Object msg){
        log.info("接收消息："+msg);
    }

    @RabbitListener(queues = "queue_fanout01")
    public void recive01(Object msg){
        log.info("queue_fanout01接收消息："+msg);
    }

    @RabbitListener(queues = "queue_fanout02")
    public void recive02(Object msg){
        log.info("queue_fanout01接收消息："+msg);
    }

    @RabbitListener(queues = "queue_direct01")
    public void recive03(Object msg){
        log.info("queue_direct01接受消息："+msg);
    }

    @RabbitListener(queues = "queue_direct02")
    public void recive04(Object msg){
        log.info("queue_direct02接收消息："+msg);
    }

    @RabbitListener(queues = "queue_topic01")
    public void recive05(Object msg){
        log.info("queue_topic01接收消息："+msg);
    }

    @RabbitListener(queues = "queue_topic02")
    public void recive06(Object msg){
        log.info("queue_topic02接收消息："+msg);
    }

    @RabbitListener(queues = "queue_header01")
    public void recive07(Message msg){
        log.info("queue_header01接收Message对象:"+msg);
        log.info("queue_header01接收消息："+new String(msg.getBody()));
    }

    @RabbitListener(queues = "queue_header02")
    public void recive08(Message msg){
        log.info("queue_header02接收Message对象:"+msg);
        log.info("queue_header02接收消息："+new String(msg.getBody()));
    }*/
}
